// @Date         2021-10-13
// @Author       Aaron Zhang
// @Description   格式化时间成各种格式
// @FileName      date_format.go

package util

import (
	"fmt"
	"strings"
	"time"
)

const (
	DateFormat1 = "2006-01-02 15:04:05"
	DateFormat2 = "2006-01-02"
	DateFormat3 = "2006/01/02 15:04:05"
	DateFormat4 = "2006/01/02"
	DateFormat5 = "2006年01月02日"
	DateFormat6 = "2006年01月02日 15点04分05秒"
)

func ConvertDate2String(dateUnix int64, format string) string {
	res := time.Unix(dateUnix, 0).Format(format)
	return res
}

func ConvertNowDate2String(format string) string {
	res := time.Unix(time.Now().Unix(), 0).Format(format)
	return res
}

func ConvertData2Unix(dateString string) int64 {
	dateString = strings.Replace(dateString, "年", "-", 1)
	dateString = strings.Replace(dateString, "/", "-", 1)
	dateString = strings.Replace(dateString, "月", "-", 1)
	dateString = strings.Replace(dateString, "日", "", 1)
	dateString = strings.Replace(dateString, "点", ":", 1)
	dateString = strings.Replace(dateString, "时", ":", 1)
	dateString = strings.Replace(dateString, "分", ":", 1)
	dateString = strings.Replace(dateString, "秒", "", 1)

	fmt.Println(dateString)
	result, err := time.ParseInLocation(DateFormat1, dateString, time.Local)
	if err != nil {
		fmt.Println(err)
		return 0
	}
	return result.Unix()
}
